package com.eternal.widget;

import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.UpdateAppearance;

/**
 * Created by Administrator
 * On 2020/4/24
 * Description:
 * @author Administrator
 */
public class ColorSpan extends CharacterStyle implements UpdateAppearance {
    private int[] colors;
    private Shader shader;

    public ColorSpan(int... colors) {
        if(colors.length==0){
            this.colors=new int[]{0xff128bec, 0xff1da9f1, 0xff29c9f7};
        }else{
            this.colors = colors;
        }
    }

    @Override
    public void updateDrawState(TextPaint textPaint) {
        if(shader==null){
            shader=new LinearGradient(0, 0, 0,textPaint.descent()-textPaint.ascent(),
                    colors,null, Shader.TileMode.REPEAT);
        }
        textPaint.setShader(shader);
    }
}
